﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;

using NHibernate;
using BEGDC.Utility.NHibernate;

namespace BEGDC.MIS.Office
{
    public class CommonUtility
    {
        private static Hashtable cache_cat;

        private static Hashtable cache_bd;

        private static Hashtable cache_province;

        private static Hashtable cache_producttype;

        public static string ConvertDataByColumn(string columnName, string columnValue, ISession session)
        {
            if (cache_bd == null || cache_cat == null || cache_province == null || cache_producttype == null)
            {
                DataTable table;
                cache_bd = new Hashtable();
                cache_cat = new Hashtable();
                cache_province = new Hashtable();
                cache_producttype = new Hashtable();

                string strSql = "select BDID, Name from BD";
                table = DAO.ExecuteDataTable(session, strSql);
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    DataRow row = table.Rows[i];
                    cache_bd.Add(row["Name"].ToString(), row["BDID"].ToString());
                }

                strSql = "select CATID, CATCode from CAT";
                table = DAO.ExecuteDataTable(session, strSql);
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    DataRow row = table.Rows[i];
                    cache_cat.Add(row["CATCode"].ToString(), row["CATID"].ToString());
                }

                strSql = "select ProvinceID, ENName from Province";
                table = DAO.ExecuteDataTable(session, strSql);
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    DataRow row = table.Rows[i];
                    cache_province.Add(row["ENName"].ToString(), row["ProvinceID"].ToString());
                }

                strSql = "select ProductTypeID, Name from ProductType";
                table = DAO.ExecuteDataTable(session, strSql);
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    DataRow row = table.Rows[i];
                    cache_producttype.Add(row["Name"].ToString(), row["ProductTypeID"].ToString());
                }
            }

            switch (columnName)
            {
                case ColumnNameList.BD:
                    return (string)cache_bd[columnValue];
                case ColumnNameList.CAT:
                    return (string)cache_cat[columnValue];
                case ColumnNameList.Province:
                    return (string)cache_province[columnValue];
                case ColumnNameList.ProductType:
                    return (string)cache_producttype[columnValue];
                default:
                    return columnValue;
            }

        }

        public static string ConvertQuarter(string columnName, string columnValue, DataRow row)
        {
            if (columnName != ColumnNameList.Quarter)
                return columnValue;
            else
            {
                int month = int.Parse(row[ColumnNameList.Month].ToString());
                return Convert.ToString((month + 2) / 3);
            }
        }

        public class ColumnNameList
        {
            public const string BD = "BD";
            public const string CAT = "CAT";
            public const string Province = "Province";
            public const string ProductType = "ProductType";
            public const string Quarter = "Quarter";
            public const string Month = "Month";
        }
    }
}
